﻿using Evision.Insure;
using Evision.Zlsoft.Miaiap.Core.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zlsy.Core.Data;

namespace Evision.Zlsoft.Miaiap.Core.Service {
   public class ModelApprovalStatus {
        //private Ado _ado;
        //public ModelApprovalStatus() {
        //    _ado = Ado.Default;
        //}
        /// <summary>
        /// 医嘱状态、医嘱撤销
        /// </summary>
        /// <param name="sendId"></param>
        /// <param name="no"></param>
        /// <param name="outorin"></param>
        /// <returns></returns>
        public static InputApprovalStatus GetApprovalStatus(Int32 adviceId, Int32 patiendId ,Int32 pageId, string no, bool outorin = true) {
            //查询门诊医嘱
            string sqlpresout = @"SELECT g.id AS 处方号,
                             f.编号 AS 开嘱医生代码
                      FROM 病人医嘱记录 g
                      JOIN 人员表 f ON g.开嘱医生 = f.姓名
                      JOIN 部门表 d ON g.开嘱科室id = d.id
                      WHERE g.id = ?";
            //门诊信费用息查询
            string sqlout = @"SELECT
                                         NVL(c.编码, b.编码) AS 项目编码
                                    FROM 
                                        病人医嘱记录 g 
                                    LEFT JOIN 
                                        收费项目目录 c ON g.收费细目id = c.id
                                    LEFT JOIN 
                                        诊疗项目目录 b ON g.诊疗项目id = b.id
                                    WHERE  
                                        g.id = ? ";
            InputApprovalStatus inputApprovalStatus = new InputApprovalStatus();
            inputApprovalStatus.hospitalizationFlag = outorin ? "op" : "ip";
            MessageBox.Show(pageId.ToString());
            inputApprovalStatus.treatmentCode = Adodb.GetScalar<string>(@"SELECT * FROM ( select a.YB流水号 from 就诊登记记录 a  where  病人id = ?  ORDER BY  a.就诊时间 DESC)  WHERE ROWNUM = 1", patiendId);
            inputApprovalStatus.prescriptions = new List<SPrescription>();
            SPrescription sPrescription = new SPrescription();
            sPrescription.medicines = new List<SMedicine>();
               //if (outorin) {
               //    //1：门诊  2：住院
               //    sqlpresout.Append(" and g.病人来源=1");
               //} else {
               //    sqlpresout.Append(" and g.病人来源=2");
               //}
               DataRow drpresout = Adodb.QueryForRow(sqlpresout,adviceId.ToString());
                sPrescription.prescriptionId = adviceId.ToString();
                DataTable dtout = Adodb.QueryForTable(sqlout, adviceId.ToString());
                foreach (DataRow rowout in dtout.Rows) {
                    SMedicine sMedicine = new SMedicine();
                    sMedicine.code = rowout["项目编码"].ToString();
                    sPrescription.medicines.Add(sMedicine);
                }
                inputApprovalStatus.prescriptions.Add(sPrescription);
            return inputApprovalStatus;
        }
    }
}
